/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.ExecAplicInterface;
import br.gov.cemaden.library.model.smp.ExecAplic;
import br.gov.cemaden.library.model.smp.PIDAplic;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class ExecAplicDao extends GenericDao implements ExecAplicInterface {

    private Session session;

    public ExecAplicDao(Session session) {
        this.session = session;
    }

    public List<ExecAplic> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ExecAplic> execAplic = null;
        try {
            Query query = session.getNamedQuery("ExecAplic.findAll");
            execAplic = (List<ExecAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecAplicDao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execAplic;
    }

    public ExecAplic findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ExecAplic execAplic = null;
        try {
            Query query = session.getNamedQuery("ExecAplic.findById").setParameter("id", id);
            execAplic = (ExecAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecAplicDao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execAplic;
    }

    public ExecAplic findByPid(Integer pid) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ExecAplic pidAplic = null;
        try {
            Query query = session.getNamedQuery("ExecAplic.findByPid").setParameter("pid", pid);
            pidAplic = (ExecAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecAplicDao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pidAplic;
    }

    public List<ExecAplic> findByData(Date data) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ExecAplic> execAplic = null;
        try {
            Query query = session.getNamedQuery("ExecAplic.findByData").setParameter("data", data);
            execAplic = (List<ExecAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecAplicDao.findByData: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execAplic;
    }

    public ExecAplic findByPidAndData(Integer pid, Date data) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ExecAplic pidAplic = null;
        try {
            Query query = session.getNamedQuery("ExecAplic.findByPidAndData")
                    .setParameter("pid", pid)
                    .setParameter("data", data);

            pidAplic = (ExecAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no PIDAplicDao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pidAplic;
    }

    public List<ExecAplic> findByPidAplic(PIDAplic pidAplic) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ExecAplic> execAplic = null;
        try {
            Query query = session.getNamedQuery("ExecAplic.findByPidAplic").setParameter("pidAplic", pidAplic);
            execAplic = (List<ExecAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecAplicDao.findByExecAplic: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execAplic;
    }
    
    public List<ExecAplic> findByPidAplicID(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ExecAplic> execAplic = null;
        try {
            String hql;
            hql = "SELECT * FROM smp.smp_exec_aplic WHERE id_pid_aplic = " + id;                   
            execAplic = session.createSQLQuery(hql).addEntity(ExecAplic.class).list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ExecAplicDao.findByPidAplicID: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return execAplic;
    }

    public ExecAplic insert(ExecAplic execAplic) {
        try {
            saveOrUpdate(this.session, execAplic);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return execAplic;
    }

    public ExecAplic update(ExecAplic execAplic) {
        try {
            saveOrUpdate(this.session, execAplic);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return execAplic;
    }

    public void delete(ExecAplic execAplic) {
        try {
            remove(this.session, execAplic);
        } catch (Exception ex) {
            Logger.getLogger(PIDAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
